package cc.autoapi.pucong.liteflow.design.controller;

import cc.autoapi.pucong.liteflow.design.core.configuration.DesignExceptionHandler;
import cc.autoapi.pucong.liteflow.design.core.configuration.LiteFlowDesignConfiguration;
import cc.autoapi.pucong.liteflow.design.core.exception.DesignException;
import cc.autoapi.pucong.liteflow.design.mobal.HeaderDesignUser;
import cc.autoapi.pucong.liteflow.design.mobal.JsonBean;
import cc.autoapi.pucong.liteflow.design.mobal.dto.UserLoginDto;
import cc.autoapi.pucong.liteflow.design.service.DesignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/flowDesign/loginApi")
public class DesignLoginController extends DesignController implements DesignExceptionHandler {
    public DesignLoginController(LiteFlowDesignConfiguration configuration) {
        super(configuration);
    }

    @GetMapping("/needLogin")
    public JsonBean<Boolean> needLogin() {
        DesignService designService = configuration.getDesignService();
        Boolean aBoolean = designService.needLogin();
        return new JsonBean<>(aBoolean);
    }

    @GetMapping("/isLogin")
    public JsonBean<Boolean> isLogin() {
        DesignService designService = configuration.getDesignService();
        Boolean aBoolean = designService.isLogin();
        return new JsonBean<>(aBoolean);
    }

    @GetMapping("/getLoginUser")
    public JsonBean<HeaderDesignUser> getLoginUser() {
        DesignService designService = configuration.getDesignService();
        HeaderDesignUser loginUser = designService.getLoginUser();
        return new JsonBean<>(loginUser);
    }

    @PostMapping("/loginOut")
    public JsonBean<Boolean> loginOut() {
        DesignService designService = configuration.getDesignService();
        Boolean aBoolean = designService.loginOut();
        return new JsonBean<>(aBoolean);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public JsonBean<HeaderDesignUser> login(@RequestBody UserLoginDto dto, HttpServletResponse response) throws DesignException {
        if (StringUtils.isBlank(dto.getLoginName()) || StringUtils.isBlank(dto.getPassword())) {
            throw new DesignException("login error");
        }
        HeaderDesignUser headerDesignUser = designService.login(dto, response);
        return new JsonBean<>(headerDesignUser);
    }
}
